aboutsummaryrefslogtreecommitdiff
path: root/pages/api/auth/[...nextauth].js
diff options
context:
space:
mode:
authorFactiven <[email protected]>2023-04-11 23:23:29 +0700
committerFactiven <[email protected]>2023-04-11 23:23:29 +0700
commit1fcdd9f7d859b925bf92265f441655d5522e351c (patch)
tree86391522f6fcc70d105f7e796a9f91d132ee4a29 /pages/api/auth/[...nextauth].js
parentInitial commit (diff)
downloadmoopa-1fcdd9f7d859b925bf92265f441655d5522e351c.tar.xz
moopa-1fcdd9f7d859b925bf92265f441655d5522e351c.zip
initial commit
Diffstat (limited to 'pages/api/auth/[...nextauth].js')
-rw-r--r--pages/api/auth/[...nextauth].js76
1 files changed, 76 insertions, 0 deletions
diff --git a/pages/api/auth/[...nextauth].js b/pages/api/auth/[...nextauth].js
new file mode 100644
index 0000000..571bfdb
--- /dev/null
+++ b/pages/api/auth/[...nextauth].js
@@ -0,0 +1,76 @@
+import NextAuth from "next-auth";
+import { GET_CURRENT_USER } from "../../../queries";
+import { client } from "../../../lib/apolloClient";
+import crypto from "crypto";
+import clientPromise from "../../../lib/mongodb";
+import { MongoDBAdapter } from "@next-auth/mongodb-adapter";
+
+const jwtSecret = crypto.randomBytes(64).toString("hex");
+
+export const authOptions = {
+ // Configure one or more authentication providers
+ adapter: MongoDBAdapter(clientPromise),
+ providers: [
+ {
+ id: "AniListProvider",
+ name: "AniList",
+ type: "oauth",
+ token: "https://anilist.co/api/v2/oauth/token",
+ authorization: {
+ url: "https://anilist.co/api/v2/oauth/authorize",
+ params: { scope: "", response_type: "code" },
+ },
+ userinfo: {
+ url: process.env.GRAPHQL_ENDPOINT,
+ async request(context) {
+ const { data } = await client.query({
+ query: GET_CURRENT_USER,
+ context: {
+ headers: {
+ Authorization: "Bearer " + context.tokens.access_token,
+ },
+ },
+ });
+
+ return {
+ token: context.tokens.access_token,
+ name: data.Viewer.name,
+ sub: data.Viewer.id,
+ image: data.Viewer.avatar,
+ };
+ },
+ },
+ clientId: process.env.CLIENT_ID,
+ clientSecret: process.env.CLIENT_SECRET,
+ profile(profile) {
+ return {
+ token: profile.token,
+ id: profile.sub,
+ name: profile?.name,
+ image: profile.image,
+ };
+ },
+ },
+ ],
+ secret: jwtSecret,
+ session: {
+ //Sets the session to use JSON Web Token
+ strategy: "jwt",
+ cookie: {
+ // Set the cookie to be secure and HTTP-only
+ secure: true,
+ httpOnly: true,
+ },
+ },
+ callbacks: {
+ async jwt({ token, user }) {
+ return { ...token, ...user };
+ },
+ async session({ session, token, user }) {
+ session.user = token;
+ return session;
+ },
+ },
+};
+
+export default NextAuth(authOptions);